package com.xingbang.controller;
import com.xingbang.pojo.CheckUnSuccessive;
import com.xingbang.service.CheckUnSuccessiveService;
import com.xingbang.service.ConsumerecordsService;
import com.xingbang.service.UserService;
import com.xingbang.uitl.constant.ConstantStr;
import com.xingbang.uitl.log.MyLogUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
/**
 * @author mingfei
 * @program payment
 * @description
 * @create 2020-04-27 15:48
 */
@Controller
public class UserController {
	private final Logger myLog = LoggerFactory.getLogger(UserController.class);
	@Resource
	private UserService userService;
	@Resource
	private ConsumerecordsService consumerecordsService;
	@Resource
	private CheckUnSuccessiveService checkUnSuccessiveService;
	public void user() {
		try {
			List<CheckUnSuccessive> checkUnSuccessiveList = checkUnSuccessiveService.selectNeedCheckUserId();
			myLog.info("checkUnSuccessiveList " + checkUnSuccessiveList);
			if(StringUtils.isEmpty(checkUnSuccessiveList) || checkUnSuccessiveList.size() <= 0) {
				myLog.info("当前没有需要检测的记录:checkUnSuccessiveList" + checkUnSuccessiveList);
				return;
			}
			// 按照 用户 检测的逻辑
			List<Integer> userIdList = new ArrayList<>();
			for (CheckUnSuccessive checkUnSuccessive : checkUnSuccessiveList) {
				userIdList.add(checkUnSuccessive.getUserId().intValue());
			}
			List<Map<Object, Object>> userMap = userService.select(userIdList);
			myLog.info("userMap = " + userMap);
			if(StringUtils.isEmpty(userMap) || userMap.size() <= 0) {
				checkUnSuccessiveService.updateByUserIds(userIdList, 2);
				myLog.info("找不到人员信息:userIds:" + userIdList);
				return;
			}
			for (Map<Object, Object> map : userMap) {
				myLog.info("map = " + map);
				if(StringUtils.isEmpty(map) || map.size() <= 0) {
					checkUnSuccessiveService.updateByUserIds(userIdList, 3);
					myLog.info("找不到记录信息:map:" + map);
					return;
				}
				if(StringUtils.isEmpty(map.get("userId")) || StringUtils.isEmpty(map.get("companyId"))) {
					checkUnSuccessiveService.updateByUserIds(userIdList, 4);
					myLog.info("找不到记录信息:userId:" + map.get("userId"));
					myLog.info("找不到记录信息:companyId:" + map.get("companyId"));
					return;
				}
				String userId = map.get("userId").toString();
				String companyId = map.get("companyId").toString();
				String sql1 = "select companyId,userId,opCount from ";
				String sql2 = "(select companyId,userId,opCount from consumerecords where companyId = %s and userId = %s order by opcount asc) t ";
				sql2 = String.format(sql2, companyId, userId);
				String sql3 = "where (select 1 from consumerecords where companyId = %s and userId = %s and opCount=t.opCount-1) is null";
				sql3 = String.format(sql3, companyId, userId);
				String userSql = sql1 + sql2 + sql3;
				myLog.info("userSql = " + userSql);
				List<Map<Object, Object>> crdMap = consumerecordsService.sql(userSql);
				myLog.info("crdMap = " + crdMap);
				StringBuilder s = new StringBuilder();
				for (Map<Object, Object> map1 : crdMap) {
					if(StringUtils.isEmpty(map1.get("opcount")) || map1.get("opCount").toString().equals(ConstantStr.STR_P2)) {
						continue;
					}
					s.append("companyId = ").append(map1.get("companyId")).append(" ");
					s.append("userId = ").append(map1.get("userId")).append(" ");
					s.append("opcount = ").append(map1.get("opcount")).append("\r\n");
				}
				if(StringUtils.isEmpty(s)) {
					myLog.info("检查结果为空：s" + s);
				}
				File file = new File("");
				String canonicalPath = file.getCanonicalPath();
				myLog.info("canonicalPath = " + canonicalPath);
				MyLogUtil.writeLogToFile(canonicalPath, "data.log", s.toString(), true);
				CheckUnSuccessive checkUnSuccessive = new CheckUnSuccessive();
				checkUnSuccessive.setCheckStatus(0);
				checkUnSuccessive.setUserId(Long.valueOf(userId));
				checkUnSuccessiveService.updateByUserIdCheckStatus(checkUnSuccessive);
			}
			// TODO 按照公司检测的逻辑
		}
		catch (Exception e) {
			myLog.error(e.getMessage(), e);
		}
	}
}
